#include <stdio.h>
#include <malloc.h>
#include <assert.h>


#include "DCList.h"

Node* BuyDListNode(DataType data)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (NULL == newNode)
    {
        assert(0);
        return NULL;

    }

    newNode->data = data;
    newNode->next = NULL;
    newNode->prev = NULL;

    return newNode;

}

void DListInit(Node** head)
{
    assert(head);
    *head = BuyDListNode(0);
    (*head)->next = *head;
    (*head)->prev = *head;

}


void DListPushFront(Node* head, DataType data)
{
    DListInsert(head->next, data);

}

void DListPopFront(Node* head)
{
    assert(head);
    DListErase(head->next);

}

void DListPushBack(Node* head, DataType data)
{
    DListInsert(head, data);

}

void DListPopBack(Node* head)
{
    assert(head);
    DListErase(head->prev);

}

void DListInsert(Node* pos, DataType data)
{
    Node* newNode = NULL;
    if (NULL == pos)
        return;

    newNode = BuyDListNode(data);
    newNode->prev = pos->prev;
    newNode->next = pos;
    newNode->prev->next = newNode;
    pos->prev = newNode;

}

void DListErase(Node* pos)
{
    if (NULL == pos)
        return;

    pos->prev->next = pos->next;
    pos->next->prev = pos->prev;

    free(pos);

}

void DListDestroy(Node** head)
{
    assert(head);
    Node* cur = (*head)->next;
    while (cur != (*head))
    {
        (*head)->next = cur->next;
        free(cur);
        cur = (*head)->next;

    }

    free(*head);
    *head = NULL;

}

void TestDList(Node* head)
{
    assert(head);
    Node* cur = head->next;
    while (cur != head)
    {
        printf("%d ", cur->data);
        cur = cur->next;

    }
    printf("\n");

}
